package com.sky.controller.admin;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-12 20:15
 */
@Api(tags = "套餐管理相关接口")
@Slf4j
@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService service;

    /**
     * 套餐分页查询相关接口
     * @param setmealPageQueryDTO
     * @return
     */
     @ApiOperation("套餐分页相关接口")
     @GetMapping("page")
    public Result<PageResult> pageSetmea(SetmealPageQueryDTO setmealPageQueryDTO  ){
         log.info("套餐分页相关接口:{}",setmealPageQueryDTO);
          PageResult pageResult= service.pageSetmea(setmealPageQueryDTO);
          return Result.success(pageResult);

     }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(value = "setmeal"  ,key = "#setmealDTO.categoryId")
    /**
     * @CacheEvict作用：删除指定缓存名称和key的缓存数据
     *      @CacheEvict(value = "userQuery",key = "#id") 只删除指定的缓存名称和key的数据
     *      @CacheEvict(value = "userQuery",allEntries = true) 删除所有缓存名称为userQuery的缓存数据
     *   	value: 缓存的名称，每个缓存名称下面可以有多个key
     *
     * 	key: 缓存的key  ----------> 支持Spring的表达式语言SPEL语法
     * @param id
     */
    public  Result addsetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("套餐添加相关接口;{}",setmealDTO);
        service.addsetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public  Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐:{}",setmealDTO);
        service.update(setmealDTO);
        return Result.success();
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public  Result<SetmealVO> getById(@PathVariable("id") Long id){
        log.info("根据id查询:{}",id);
        SetmealVO setmealVO= service.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 起售停售状态修改
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("起售停售状态修改")
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public  Result updatestatus(@PathVariable Integer status,Long id){
        log.info("起售停售状态修改:{},{}",status,id);
        service.updatestatus(status,id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result deletes( @RequestParam List<Long> ids){
        log.info("删除套餐:{}",ids);
        service.deletes(ids);
        return Result.success();
    }
}
